16.2 Function Tracer Module Initialization
The initialization function for the function tracing infrastructure is hosted within the source tree file git/kernel/trace/trace.c, and its definition is implemented as follows:
void __init ftrace_init(void)
{
extern unsigned long __start_mcount_loc[];
extern unsigned long __stop_mcount_loc[];
unsigned long count, flags;
int ret;
local_irq_save(flags);
ret = ftrace_dyn_arch_init();
local_irq_restore(flags);
if (ret)
goto failed;
count = __stop_mcount_loc - __start_mcount_loc;
if (!count) {
pr_info("ftrace: No functions to be traced?\n");
goto failed;
}
pr_info("ftrace: allocating %ld entries in %ld pages\n", count, count / ENTRIES_PER_PAGE + 1);
last_ftrace_enabled = ftrace_enabled = 1;
ret = ftrace_process_locs(NULL, __start_mcount_loc, __stop_mcount_loc);
pr_info("ftrace: allocated %ld pages with %ld groups\n",
ftrace_number_of_pages, ftrace_number_of_groups);
set_ftrace_early_filters();
return;
failed:
ftrace_disabled = 1;
}
The routine local_irq_save(flags) disables hardware interrupts on the local CPU core and flushes the current interrupt state configuration mask into the tracking variable flags.
The function ftrace_dyn_arch_init() is highly dependent on the target CPU architecture. It executes the platform-specific low-level bootstrap operations required by the Dynamic Ftrace engine, and its primary technical objective is to prepare the environment for dynamic code modifications (Runtime Patching) on the active architecture. Early function tracking code sequences encompassed milestones such as:
-
Initializing architecture-specific tracking metrics and processor states via ftrace_dyn_arch_init().
-
Overwriting the mcount() or __fentry() function entry boundaries with NOP hardware instructions via ftrace_make_nop().
-
Configuring the redirection branch blueprints via ftrace_make_call().
-
Updating the destination execution patch targets via ftrace_update_ftrace_func().
Because modern processor architectures incorporate robust hardware-level support for function tracing, and modern compilers generate a comprehensive array table tracking the addresses of all instrumented functions at compile time (situated within the dedicated __mcount_loc memory segment), the function tracer module merely needs to parse this pre-compiled table and execute a batched sweep to invoke ftrace_make_nop. This "static table + dynamic remapping" model is universal across platforms, meaning the architectural layer inside ftrace_dyn_arch_init() no longer requires complex runtime runtime inspection. This optimization is especially prominent on the ARM architecture, where instructions feature a fixed 32-bit alignment and the branch instructions (such as bl) leverage specialized encoding formats. Consequently, for both the x86 and ARM architectures, the ftrace_dyn_arch_init() routine resolves to an empty function block.
The macro local_irq_restore(flags) utilizes the saved state archived inside the flags variable to restore the CPU core's interrupt configuration back to its status prior to calling local_irq_save(), thereby re-enabling local hardware interrupt processing.
After computing the absolute magnitude of the __mcount_loc block (bounded between the linker symbols __start_mcount_loc and __stop_mcount_loc), the ftrace_init() routine calls the function ftrace_process_locs(). Leveraging the address tables preserved within __mcount_loc, it batch-overwrites the call mcount or fentry instructions at those explicit locations with hardware NOP instructions. Its primary operations encompass:
-
Locating the starting and ending address boundaries of the __mcount_loc section. This tracking table is generated at compile time by kernel build utilities (such as scripts/recordmcount), and logs the entry code offsets for every single traceable function using an array layout described by struct ftrace_page descriptors.
-
Sorting the extracted instruction addresses in ascending order to facilitate fast binary search lookups while pruning duplicate or structurally invalid entries.
-
Iterating through every address inside the table to trigger code updates via ftrace_update_code(), which invokes ftrace_nop_initialize(). ftrace_nop_initialize() subsequently routes the call to the architecture-specific wrapper ftrace_make_nop() or ftrace_init_nop() (an interface explicitly implemented under ARM64), which overwrites the original compiler profiling hooks with hardware NOP instructions while marking the timing boundaries of the code hot-patch loop.
At the conclusion of the ftrace_init() routine, the system calls set_ftrace_early_filters() to apply the early parameter filters passed during boot before starting function tracing operations.